package com.crazecoder.flutter.iflyad.utils;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

//import com.github.gzuliyujiang.oaid.DeviceID;
//import com.github.gzuliyujiang.oaid.IDeviceId;
//import com.github.gzuliyujiang.oaid.IOAIDGetter;

import java.util.UUID;

public class OAIDUtil implements MiitHelper.AppIdsUpdater {
    private static OAIDUtil instance;
    private String oaid;
    private Context context;
    private MiitHelper miitHelper;

    private OAIDUtil(Context context) {
        this.context = context;
        miitHelper = new MiitHelper(this);
    }

    public static OAIDUtil getInstance(Context context) {
        if (instance == null) {
            instance = new OAIDUtil(context);
        }
        return instance;
    }

    /**
     * 获取oaid
     */
    public void init() {
        if (TextUtils.isEmpty(oaid))
            miitHelper.getDeviceIds(context);
//        IDeviceId deviceId = DeviceID.with(context);
//        if (!deviceId.supportOAID()) {
//            // 不支持OAID，须自行生成全局唯一标识（GUID）。
//            // 本库不提供GUID的生成方式，可以使用`UUID.randomUUID().toString()`生成，
//            // 然后存到`SharedPreferences`及`ExternalStorage`甚至`CloudStorage`。
//            return;
//        }
//        deviceId.doGet(new IOAIDGetter() {
//            @Override
//            public void onOAIDGetComplete(@NonNull String oaid) {
//                OAIDUtil.this.oaid = oaid;
//            }
//
//            @Override
//            public void onOAIDGetError(@NonNull Exception exception) {
//
//            }
//        });
    }

    public String getOAID() {
        final String androidId = Settings.Secure.getString(
                context.getContentResolver(), Settings.Secure.ANDROID_ID);
        return oaid == null ? androidId : oaid;
    }

    @Override
    public void OnIdsAvalid(@NonNull String oaid) {
        this.oaid = oaid;
    }
}
